package com.bw.cashier.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.cashier.entity.AddChargeEntity
import com.bw.cashier.entity.BillEntity
import com.bw.cashier.entity.CashierListEntityItem
import com.bw.cashier.entity.ChargeDataEntityItem
import com.bw.cashier.entity.ChargeDetailEntity
import com.bw.cashier.module.CashierRepository
import com.bw.common.vm.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class CashierViewModel  @Inject constructor(private val repo :CashierRepository):BaseViewModel(){
    //定义Livedata缓存数据
    val mCashierSuccess = MutableLiveData<List<CashierListEntityItem>>()
    fun getCashierData(id:Int){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadCashierData(id)
            if (res.code == 200){
                mCashierSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }
    //获取账单详情
    val mBillSuccess = MutableLiveData<BillEntity>()
    fun getBillData(id:Int){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadBillDetail(id)
            if (res.code == 200){
                mBillSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }
    //获取详情页内的收费数据
    val mChargeSuccess = MutableLiveData<List<ChargeDataEntityItem>>()
    fun getChargeData(id:Int){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadChargeData(id)
            if (res.code == 200){
                mChargeSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }
    //获取收费详情页面
    val mChargeDetailSuccess = MutableLiveData<ChargeDetailEntity>()
    fun getChargeDetail(id: Int){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadChargeDetail(id)
            if (res.code ==200){
                mChargeDetailSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }
    //添加收费
    val mAddChargeSuccess = MutableLiveData<AddChargeEntity>()
    fun addChargeData(addChargeEntity: AddChargeEntity){
        viewModelScope.launch(mDispatcher) {
            val res = repo.addChargeData(addChargeEntity)
            if (res.code ==200){
                mAddChargeSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }
}